{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# https://www.heywhale.com/mw/project/59e389b54663f7655c48f518\n",
    "# https://www.heywhale.com/mw/project/59e77a636d213335f38daec2/content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "current_dir = os.path.abspath(r'')\n",
    "# os.getcwd()\n",
    "# up_dir = os.path.dirname(os.path.abspath(__file__))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path1 = os.path.join(current_dir,\"exercise_data\",\"chipotle.tsv\")\n",
    "chipo = pd.read_csv(path1,sep = '\\t')\n",
    "chipo.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#行 列\n",
    "chipo.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列名\n",
    "# 该列的数据是由 Python 对象构成的，而不是由基本数据类型（如整数、浮点数等）构成的。对象类型通常是用来存储字符串类型、日期类型或者混合类型数据的。\n",
    "chipo.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chipo.describe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chipo.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 每列 非空个数\n",
    "chipo.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#按照某列降序（默认升序）\n",
    "chipo.sort_values(\"item_price\",ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对item_name 进行分组 并取quantity进行计数\n",
    "\n",
    "# 这里c 是series \n",
    "c=chipo.groupby('item_name')['quantity'].sum()\n",
    "print(type(c))\n",
    "# 排序\n",
    "d = c.sort_values(ascending=False)\n",
    "d.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# agg({'quantity':sum}) 聚合\n",
    "c = chipo[[\"item_name\",\"quantity\"]].groupby(\"item_name\").agg({'quantity':sum})\n",
    "\n",
    "d = chipo.groupby(\"item_name\")[['item_name','quantity']]\n",
    "print(type(c))\n",
    "print(type(d))\n",
    "\n",
    "c.sort_values(\"quantity\",ascending=False).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nmlike_venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
